package com.scsj.supermarket.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.scsj.supermarket.R;
import com.scsj.supermarket.event.FirstEvent;
import com.scsj.supermarket.utils.RxBus;
import com.scsj.supermarket.view.activity.serchmodel.SearchResultActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

import es.dmoral.toasty.Toasty;

/**
 * 搜索结果页面选择
 */

public class SelectMarkAdapter extends RecyclerView.Adapter<SelectMarkAdapter.ViewHolderA> {

    public static String selectMark= null;
    //是否点击了清空按钮
    public static boolean isClearAllMark= false;

    private Context mContext;
    private List<String> mList;
    private List<Boolean> isClicks;//控件是否被点击,默认为false，如果被点击，改变值，控件根据值改变自身颜色
    public int currentPosition;

    public SelectMarkAdapter(Context context, List<String> list,int sortposion) {
        mContext = context;
        mList = list;

        isClicks = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            isClicks.add(false);
        }
        if(sortposion != -1){
            isClicks.set(sortposion,true);
        }
        currentPosition = sortposion;
    }

    @Override
    public ViewHolderA onCreateViewHolder(ViewGroup parent, int viewType) {
        //此处动态加载ViewHolder的布局文件并返回holder
        View view = LayoutInflater.from(mContext).inflate(R.layout.mark_item_layout, parent, false);
        ViewHolderA holderA = new ViewHolderA(view);
        return holderA;
    }

    @Override
    public void onBindViewHolder(final ViewHolderA holder, final int position) {

        holder.lineView.setVisibility(View.GONE);
        //此处设置Item中view的数据
        holder.mTextView.setText(mList.get(position));
        if (isClicks.get(position)) {
            holder.mTextView.setTextColor(Color.parseColor("#FF314A"));
            holder.mTextView.setBackgroundResource(R.drawable.mark_selected_shape);
        } else {
            holder.mTextView.setTextColor(Color.parseColor("#414141"));
            holder.mTextView.setBackgroundResource(R.drawable.mark_unselected_shape);
        }
        if (isClearAllMark){
            clearAllSelectedState();
        }
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearAllSelectedState();
                if(currentPosition != position) {
                    isClicks.set(position, true);
                    selectMark = mList.get(position);
                    isClearAllMark = false;
                    currentPosition = position;
                }else {
                    isClearAllMark = true;
                    currentPosition = -1;
                    selectMark = null;
                }

                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        //生成的item的数量
        return mList.size();
    }
    //Item的ViewHolder以及item内部布局控件的id绑定
    class ViewHolderA extends RecyclerView.ViewHolder {
        TextView mTextView;
        View lineView;
        public ViewHolderA(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.mark_name);
            lineView = itemView.findViewById(R.id.single_line_view);
        }
    }
    //清空选中的所有按钮
    private void clearAllSelectedState(){
        for (int i = 0; i < isClicks.size(); i++) {
            isClicks.set(i, false);
        }
    }
}


